home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June (Extra) / CHIP 2006-06.3.iso / program / opensource / clamav-devel.exe / examples / ex1.c
Encoding:
C/C++ Source or Header  |  2006-05-16  |  2.6 KB  |  93 lines

  1. /*
  2.  *  Compilation: gcc -Wall ex1.c -o ex1 -lclamav
  3.  *
  4.  *  Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  *  MA 02110-1301, USA.
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <clamav.h>
  30.  
  31. int main(int argc, char **argv)
  32. {
  33.     int fd, ret, no = 0;
  34.     unsigned long int size = 0;
  35.     long double mb;
  36.     const char *virname;
  37.     struct cl_node *root = NULL;
  38.     struct cl_limits limits;
  39.  
  40.  
  41.     if(argc != 2) {
  42.     printf("Usage: %s file\n", argv[0]);
  43.     exit(2);
  44.     }
  45.  
  46.     if((fd = open(argv[1], O_RDONLY)) == -1) {
  47.     printf("Can't open file %s\n", argv[1]);
  48.     exit(2);
  49.     }
  50.  
  51.     /* load all available databases from default directory */
  52.  
  53.     if((ret = cl_loaddbdir(cl_retdbdir(), &root, &no))) {
  54.     printf("cl_loaddbdir: %s\n", cl_perror(ret));
  55.     close(fd);
  56.     exit(2);
  57.     }
  58.  
  59.     printf("Loaded %d signatures.\n", no);
  60.  
  61.     /* build engine */
  62.     if((ret = cl_build(root))) {
  63.     printf("Database initialization error: %s\n", cl_strerror(ret));;
  64.     cl_free(root);
  65.     close(fd);
  66.     exit(2);
  67.     }
  68.  
  69.     /* set up archive limits */
  70.     memset(&limits, 0, sizeof(struct cl_limits));
  71.     limits.maxfiles = 1000; /* max files */
  72.     limits.maxfilesize = 10 * 1048576; /* maximal archived file size == 10 Mb */
  73.     limits.maxreclevel = 5; /* maximal recursion level */
  74.     limits.maxratio = 200; /* maximal compression ratio */
  75.     limits.archivememlim = 0; /* disable memory limit for bzip2 scanner */
  76.  
  77.     /* scan descriptor */
  78.     if((ret = cl_scandesc(fd, &virname, &size, root, &limits, CL_SCAN_STDOPT)) == CL_VIRUS)
  79.     printf("Virus detected: %s\n", virname);
  80.     else {
  81.     printf("No virus detected.\n");
  82.     if(ret != CL_CLEAN)
  83.         printf("Error: %s\n", cl_perror(ret));
  84.     }
  85.     close(fd);
  86.  
  87.     mb = size * (CL_COUNT_PRECISION / 1024) / 1024.0;
  88.     printf("Data scanned: %2.2Lf Mb\n", mb);
  89.  
  90.     cl_free(root);
  91.     exit(ret == CL_VIRUS ? 1 : 0);
  92. }
  93.